import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';

class AliPayPlugs{
  static const MethodChannel _aliPayChannel = MethodChannel("flutter_ali_pay_plugs");

  static Future doPay(String orderInfo,{ValueChanged<bool>? changed})async{
    try{
      if (Platform.isIOS){
        _aliPayChannel.setMethodCallHandler((call) {
          switch(call.method){
            case "onPaySuccess":
              changed?.call(true);
              break;
            case "onPayFail":
              changed?.call(false);
              Fluttertoast.showToast(msg: "支付失败,code=${call.arguments["code"]},error=${call.arguments["errorMsg"]}");
              break;
          }
          return Future.value(true);
        });
      }
      await _aliPayChannel.invokeMethod("doPay",{"orderInfo":orderInfo});
    }catch(e){
      if(e is PlatformException && e.message!=null){
        Fluttertoast.showToast(msg: e.message!);
      }
      return Future.error(e);

    }
  }
}